/*
 * listener.cpp for GodSpeaker
 *
 *
 * Author: healich@gmail.com
 */
#include <qlistbox.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include <qlayout.h>
#include "listener.h"

Listener::Listener( QWidget *parent, const char *name, WFlags f )
    : QWidget( parent, name, f )
{
    totalItems = 0;
    curItem = -1;

    //create widgets
    msglistBox = new QListBox(this);
    msglistBox->setMinimumWidth(100);

    delBtn = new QPushButton(tr("D"), this);
    delBtn->setEnabled(false);
    connect(delBtn, SIGNAL(clicked()), this, SLOT(delItem()));

    prevBtn = new QPushButton(tr("<"), this);
    prevBtn->setEnabled(false);
    connect(prevBtn, SIGNAL(clicked()), this, SLOT(prevItem()));

    nextBtn = new QPushButton(tr(">"), this);
    nextBtn->setEnabled(false);
    connect(nextBtn, SIGNAL(clicked()), this, SLOT(nextItem()));

//test
testBtn = new QPushButton(tr("Test"), this);
connect(testBtn, SIGNAL(clicked()), this, SLOT(testClicked()));
//connect(delBtn, SIGNAL(clicked()), this, SLOT(delOneChar()));
//end test
    msgLabel = new QLabel(tr("This is a text label<br>Hello World!"), this);
    msgLabel->setMinimumHeight(100);

    //connect(msglistBox, SIGNAL(selected(const QString &)), msgLabel, SLOT(setText(const QString &)));
//This signal is emitted when the user double-clicks on an item or presses Enter on the current item
//This signal is emitted when the user makes a new item the current item
    connect(msglistBox, SIGNAL(highlighted(int)), this, SLOT(listSelected(int)));

    //put widgets into layout
    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->addWidget(delBtn);
    hLayout->addWidget(prevBtn);
    hLayout->addWidget(nextBtn);
hLayout->addWidget(testBtn);
    hLayout->addStretch(1);

    QVBoxLayout *vLayout = new QVBoxLayout(this);
    vLayout->addWidget(msglistBox);
    vLayout->addLayout(hLayout);
    vLayout->addWidget(msgLabel);

}

Listener::~Listener()
{
}

void Listener::testClicked()
{
    //add some items to QListBox msglistBox
    //int i = 0;
    //while( ++i <= 5 )
    //    msglistBox->insertItem( QString::fromLatin1( "Item " ) + QString::number( i ),
    //                   i );
    while( totalItems < 5 ) {
        msglistBox->insertItem( QString::fromLatin1( "Item " ) + QString::number( totalItems ),
                       totalItems );
        ++totalItems;
    }
    delBtn->setEnabled(true);
    prevBtn->setEnabled(true);
    nextBtn->setEnabled(true);
}

void Listener::addListItem(const QString &strItem)
{
    msglistBox->insertItem( strItem,  ++totalItems);
}

void Listener::listSelected(int i)
{
//printf("hhe");
    curItem = i;    //msglistBox->currentItem ()
    QString str = msglistBox->text(i);			//QString currentText () const
    msgLabel->setText(QString::number( curItem ));
    //msgLabel->setText(str);

    nextBtn->setEnabled(true);
    prevBtn->setEnabled(true);

    if ((totalItems - 1) == i) {
        nextBtn->setEnabled(false);
    }
    if (0 == i) {
        prevBtn->setEnabled(false);
    }
}

void Listener::delItem()
{
    if (0 >= totalItems || 0 > curItem)
        return;
//printf("1: %d\n", curItem);
    msglistBox->removeItem(curItem);
//printf("2: %d\n", curItem);
    --totalItems;
    if (0 == totalItems) {
        curItem = -1;
        delBtn->setEnabled(false);
        prevBtn->setEnabled(false);
        nextBtn->setEnabled(false);
        return;
    }

    if (curItem == totalItems) {
        --curItem;
        nextBtn->setEnabled(false);
    } //else if ( 0 == curItem ){
        //++curItem;
        //prevBtn->setEnabled(false);
    //}
//printf("3: %d\n", curItem);
    //msglistBox->setCurrentItem(curItem);
    //todo: change msglabel text
    msglistBox->setSelected(curItem, true);
}

void Listener::prevItem()
{
    if ( curItem <= 0 )
        return;
    //msglistBox->setCurrentItem(--curItem);
    msglistBox->setSelected(--curItem, true);

    if ( 0 == curItem )
        prevBtn->setEnabled(false);
    if ((totalItems - 2) == curItem)
        nextBtn->setEnabled(true);
}

void Listener::nextItem()
{
    if ( (totalItems - 1) == curItem )
        return;
    //msglistBox->setCurrentItem(++curItem);
    msglistBox->setSelected(++curItem, true);

    if ((totalItems - 1) == curItem)
        nextBtn->setEnabled(false);
    if (1 == curItem)
        prevBtn->setEnabled(true);
}



